package com.zoe.onelink.redis.autoconfigure;

import com.zoe.onelink.plugin.check.EnvironmentChecker;
import com.zoe.onelink.redis.checker.OnelinkRedisEnvironmentChecker;
import com.zoe.onelink.redis.property.RedisMultiDatasourceProperties;
import com.zoe.onelink.redis.util.LettuceWatcherUtil;
import com.zoe.onelink.redis.util.RedisUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-09-11
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(RedisMultiDatasourceProperties.class)
public class OnelinkRedisAutoConfiguration {

    @Bean
    public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate, StringRedisTemplate stringRedisTemplate) {
        return new RedisUtil(redisTemplate, stringRedisTemplate);
    }

    @Bean
    @ConditionalOnClass(name = {"org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory", "io.lettuce.core.event.Event"})
    public LettuceWatcherUtil lettuceWatcherUtil(ApplicationContext applicationContext, RedisProperties redisProperties) {
        return new LettuceWatcherUtil(applicationContext, redisProperties);
    }

    @Bean
    public EnvironmentChecker onelinkRedisEnvironmentChecker(RedisProperties redisProperties) {
        return new OnelinkRedisEnvironmentChecker(redisProperties);
    }

}
